package org.crc.myvhrb.controller.system.basic;

import org.crc.myvhrb.model.Department;
import org.crc.myvhrb.model.RespBean;
import org.crc.myvhrb.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CRC
 * 2021/8/11
 */
@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {


    @Autowired
    DepartmentService departmentService;

    // 查询所有部门数据
    @GetMapping("/")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartments();
    }

    // 添加部门，这里使用到了存储过程
    @PostMapping("/")
    public RespBean addDep(@RequestBody Department dep){
        departmentService.addDep(dep);
        if (dep.getResult() == 1){
            return RespBean.success("添加成功",dep);
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteDepById(@PathVariable Integer id){
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteById(dep);
        if (dep.getResult() == -2){
            return RespBean.error("该部门下有子部门，删除失败");
        }else if (dep.getResult() == -1){
            return RespBean.error("该部门下有员工，删除失败");
        }else if (dep.getResult() == 1){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
